
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyIM.client.callback;

import MyIM.client.Client;
import MyIM.client.engine.Engine;
import MyIM.client.view.form.Dialog_Chat;
import MyIM.client.view.form.Dialog_Conference;
import MyIM.client.view.form.Dialog_Confirm;
import MyIM.component.Friend;
import MyIM.component.Group;
import MyIM.utilities.Status;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JOptionPane;

/**
 *
 * @author satthuvdh
 */
// TODO finish here
public class CallBackRemoteObject extends UnicastRemoteObject implements CallBackRemote {

    private Engine engine;

    public CallBackRemoteObject(Engine engine) throws RemoteException {
        this.engine = engine;
    }

    @Override
    public void addFriend(Friend friend, int gid) throws RemoteException {
        ArrayList<Group> groupList = engine.getClient().getFriendList();
        for (Group group : groupList) {
            if (group.getGroupId() == gid) {
                group.addFriend(friend);
            }
        }

        engine.getClient().update(null, gid);
    }

    @Override
    public void removeFriend(int fid) throws RemoteException {
        ArrayList<Group> groupList = engine.getClient().getFriendList();
        for (Group group : groupList) {
            ArrayList<Friend> friends = group.getFriends();
            for (Friend f : friends) {
                if (f.getUid() == fid) {
                    friends.remove(f);
                }
            }
        }
        engine.getClient().update(null, fid);
    }

    @Override
    public void addMessage(int from, int to, String message) throws RemoteException {

        HashMap<Integer, Dialog_Chat> listChat = engine.getClient().getListChat();

        if (engine.getClient().getUID() == from) {

            listChat.get(to).update(null, engine.getClient().getUsername() + ":" + message);
        } else if (engine.getClient().getUID() == to) {
            if (listChat.containsKey(from)) {
                listChat.get(from).update(null, engine.getFriend(from).getUsername() + ":" + message);
            } else {
                engine.getClient().addNewDialogChat(from, new Dialog_Chat(engine, engine.getClient().getDisplayName(), engine.getClient().getAvatar(), engine.getFriend(from)));
                listChat.get(from).update(null, engine.getFriend(from).getUsername() + ":" + message);
            }

        }
    }

    @Override
    public void initFriendList(String username) throws RemoteException {
        engine.initFriendList(username);
        engine.getClient().getView().getContactView().resetFriendsList();
    }

    @Override
    public void friendOnline(int fid) throws RemoteException {
        System.out.println("request online update");
        ArrayList<Group> groups = engine.getClient().getFriendList();
        for (Group g : engine.getClient().getFriendList()) {
            ArrayList<Friend> friends = g.getFriends();
            for (Friend f : friends) {
                if (f.getUid() == fid) {
                    f.setStatus(true);
                }
            }
        }
        engine.getClient().getView().getContactView().resetFriendsList();
//        engine.initFriendList(engine.getFriend(fid).getUsername());
//        engine.getClient().getView().getContactView().resetFriendsList();
    }

    @Override
    public void friendOffline(int fid) throws RemoteException {
        ArrayList<Group> groups = engine.getClient().getFriendList();
        for (Group g : engine.getClient().getFriendList()) {
            ArrayList<Friend> friends = g.getFriends();
            for (Friend f : friends) {
                if (f.getUid() == fid) {
                    f.setStatus(false);
                }
            }
        }
        engine.getClient().getView().getContactView().resetFriendsList();
    }

    // Server send request add friend
    @Override
    public void requestAddFriend(int uid, int fid, int gid) throws RemoteException {
        String username = engine.getServer().getUsernameById(uid);
        if (username != null) {
            System.out.println(username + ": be my friend ^_^ ?");

            //User Dialog_Confirm
            engine.getClient().addNewDialogConfirm(new Dialog_Confirm(engine, username + ": " + engine.getClient().getUsername() + " be my friend ^_^ ?", uid, fid, gid));
            //getView().createDialog(username + ": be my friend ^_^ ?", uid, fid, gid);



//            int c = engine.getClient().getView().createDialog(username + ": be my friend ^_^ ?");
//
//            switch (c) {
//                case JOptionPane.YES_OPTION:
//                    System.out.println("Add Friend");
//                    engine.getServer().addFriend(uid, fid, gid);
//                    break;
//                case JOptionPane.NO_OPTION:
//                    break;
//                case JOptionPane.CLOSED_OPTION:
//                    break;
//                default:
//                    throw new AssertionError();
//            }
        }
    }

    @Override
    public void acceptAddFriend(String requested) throws RemoteException {
        engine.getClient().setFriendList(engine.getServer().getFriendList(engine.getClient().getUID()));
        engine.getClient().getView().getContactView().resetFriendsList();
        //engine.
        JOptionPane.showMessageDialog(engine.getClient().getView(), "Hey " + engine.getClient().getUsername() + "! " + requested + " accepted friend request");
    }

    @Override
    public void addGroup(int gid, String groupName) throws RemoteException {
        ArrayList<Group> listGroup = engine.getClient().getFriendList();
        listGroup.add(new Group(engine.getClient().getUID(), gid, groupName, new ArrayList<Friend>()));

        engine.getClient().update(null, gid);
    }

    @Override
    public void addMessageConference(int ownerid, String confereceName, int from, String message) throws RemoteException {
        engine.getClient().getListConference().get(ownerid + "" + confereceName).update(null, engine.getFriend(from).getDisplayName() + ":" + message);
    }

    @Override
    public Status addToConferene(int ownerid, String conferenceName, ArrayList<Friend> attendees) throws RemoteException {
        int option = JOptionPane.showConfirmDialog(null, "" + engine.getFriend(ownerid).getUsername() + " wants to add you to a conference, Do you agree");
        if (option == JOptionPane.YES_OPTION) {
            attendees.add(engine.getFriend(engine.getClient().getUID()));
            engine.getClient().getListConference().put(ownerid + "" + conferenceName, new Dialog_Conference(engine, ownerid, conferenceName, attendees));
            return Status.SUCCESS;
        } else {
            return Status.FAIL;
        }


    }

    @Override
    public void initConference(int ownerid, String conferenceName, ArrayList<Friend> attendees) {

        HashMap<String, Dialog_Conference> conferences = engine.getClient().getListConference();
        conferences.get(ownerid + "" + conferenceName).setAttendees(attendees);
        conferences.get(ownerid + "" + conferenceName).initAttendees();
    }
}
